import { 
  DIAGRAM_TYPES, 
  ELEMENT_TYPES, 
  ACTIVITY_TYPES,
  PARTICIPANT_TYPES,
  USECASE_TYPES,
  STATE_TYPES,
  RELATIONSHIP_TYPES 
} from './constants.js'

/**
 * 类图模板
 */
export const CLASS_DIAGRAM_TEMPLATE = {
  type: DIAGRAM_TYPES.CLASS,
  elements: [
    {
      id: 'class1',
      type: ELEMENT_TYPES.CLASS,
      name: 'User',
      x: 100,
      y: 100,
      attributes: [
        { name: 'id', type: 'int', visibility: 'private' },
        { name: 'name', type: 'String', visibility: 'private' },
        { name: 'email', type: 'String', visibility: 'private' }
      ],
      methods: [
        { name: 'getId', returnType: 'int', visibility: 'public' },
        { name: 'getName', returnType: 'String', visibility: 'public' },
        { name: 'setName', returnType: 'void', visibility: 'public' }
      ]
    },
    {
      id: 'class2',
      type: ELEMENT_TYPES.CLASS,
      name: 'Order',
      x: 300,
      y: 100,
      attributes: [
        { name: 'orderId', type: 'int', visibility: 'private' },
        { name: 'userId', type: 'int', visibility: 'private' },
        { name: 'total', type: 'double', visibility: 'private' }
      ],
      methods: [
        { name: 'getOrderId', returnType: 'int', visibility: 'public' },
        { name: 'calculateTotal', returnType: 'double', visibility: 'public' }
      ]
    },
    {
      id: 'interface1',
      type: ELEMENT_TYPES.INTERFACE,
      name: 'UserService',
      x: 100,
      y: 300,
      methods: [
        { name: 'createUser', returnType: 'User', visibility: 'public' },
        { name: 'findUser', returnType: 'User', visibility: 'public' },
        { name: 'updateUser', returnType: 'boolean', visibility: 'public' }
      ]
    },
    {
      id: 'relationship1',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'class1',
      targetId: 'class2',
      relationshipType: RELATIONSHIP_TYPES.ASSOCIATION,
      name: 'places'
    },
    {
      id: 'relationship2',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'class1',
      targetId: 'interface1',
      relationshipType: RELATIONSHIP_TYPES.IMPLEMENTATION
    }
  ]
}

/**
 * 活动图模板
 */
export const ACTIVITY_DIAGRAM_TEMPLATE = {
  type: DIAGRAM_TYPES.ACTIVITY,
  elements: [
    {
      id: 'start1',
      type: ELEMENT_TYPES.ACTIVITY,
      activityType: ACTIVITY_TYPES.START,
      name: '开始',
      x: 100,
      y: 50
    },
    {
      id: 'action1',
      type: ELEMENT_TYPES.ACTIVITY,
      activityType: ACTIVITY_TYPES.ACTION,
      name: '用户登录',
      x: 100,
      y: 150
    },
    {
      id: 'decision1',
      type: ELEMENT_TYPES.ACTIVITY,
      activityType: ACTIVITY_TYPES.DECISION,
      name: '验证成功？',
      x: 100,
      y: 250
    },
    {
      id: 'action2',
      type: ELEMENT_TYPES.ACTIVITY,
      activityType: ACTIVITY_TYPES.ACTION,
      name: '显示主页',
      x: 300,
      y: 250
    },
    {
      id: 'action3',
      type: ELEMENT_TYPES.ACTIVITY,
      activityType: ACTIVITY_TYPES.ACTION,
      name: '显示错误信息',
      x: 100,
      y: 350
    },
    {
      id: 'end1',
      type: ELEMENT_TYPES.ACTIVITY,
      activityType: ACTIVITY_TYPES.END,
      name: '结束',
      x: 300,
      y: 350
    },
    {
      id: 'relationship1',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'start1',
      targetId: 'action1',
      relationshipType: RELATIONSHIP_TYPES.ASSOCIATION
    },
    {
      id: 'relationship2',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'action1',
      targetId: 'decision1',
      relationshipType: RELATIONSHIP_TYPES.ASSOCIATION
    },
    {
      id: 'relationship3',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'decision1',
      targetId: 'action2',
      relationshipType: RELATIONSHIP_TYPES.ASSOCIATION,
      name: 'yes'
    },
    {
      id: 'relationship4',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'decision1',
      targetId: 'action3',
      relationshipType: RELATIONSHIP_TYPES.ASSOCIATION,
      name: 'no'
    },
    {
      id: 'relationship5',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'action2',
      targetId: 'end1',
      relationshipType: RELATIONSHIP_TYPES.ASSOCIATION
    },
    {
      id: 'relationship6',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'action3',
      targetId: 'end1',
      relationshipType: RELATIONSHIP_TYPES.ASSOCIATION
    }
  ]
}

/**
 * 时序图模板
 */
export const SEQUENCE_DIAGRAM_TEMPLATE = {
  type: DIAGRAM_TYPES.SEQUENCE,
  elements: [
    {
      id: 'actor1',
      type: ELEMENT_TYPES.LIFELINE,
      participantType: PARTICIPANT_TYPES.ACTOR,
      name: '用户',
      x: 100,
      y: 100
    },
    {
      id: 'participant1',
      type: ELEMENT_TYPES.LIFELINE,
      participantType: PARTICIPANT_TYPES.PARTICIPANT,
      name: '登录控制器',
      x: 300,
      y: 100
    },
    {
      id: 'participant2',
      type: ELEMENT_TYPES.LIFELINE,
      participantType: PARTICIPANT_TYPES.PARTICIPANT,
      name: '用户服务',
      x: 500,
      y: 100
    },
    {
      id: 'database1',
      type: ELEMENT_TYPES.LIFELINE,
      participantType: PARTICIPANT_TYPES.DATABASE,
      name: '用户数据库',
      x: 700,
      y: 100
    },
    {
      id: 'relationship1',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'actor1',
      targetId: 'participant1',
      relationshipType: RELATIONSHIP_TYPES.ASSOCIATION,
      name: '输入用户名密码'
    },
    {
      id: 'relationship2',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'participant1',
      targetId: 'participant2',
      relationshipType: RELATIONSHIP_TYPES.ASSOCIATION,
      name: '验证用户'
    },
    {
      id: 'relationship3',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'participant2',
      targetId: 'database1',
      relationshipType: RELATIONSHIP_TYPES.ASSOCIATION,
      name: '查询用户信息'
    },
    {
      id: 'relationship4',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'database1',
      targetId: 'participant2',
      relationshipType: RELATIONSHIP_TYPES.DEPENDENCY,
      name: '返回用户数据'
    },
    {
      id: 'relationship5',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'participant2',
      targetId: 'participant1',
      relationshipType: RELATIONSHIP_TYPES.DEPENDENCY,
      name: '返回验证结果'
    },
    {
      id: 'relationship6',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'participant1',
      targetId: 'actor1',
      relationshipType: RELATIONSHIP_TYPES.DEPENDENCY,
      name: '显示登录结果'
    }
  ]
}

/**
 * 用例图模板
 */
export const USECASE_DIAGRAM_TEMPLATE = {
  type: DIAGRAM_TYPES.USECASE,
  elements: [
    {
      id: 'actor1',
      type: ELEMENT_TYPES.USECASE,
      usecaseType: USECASE_TYPES.ACTOR,
      name: '用户',
      x: 100,
      y: 200
    },
    {
      id: 'actor2',
      type: ELEMENT_TYPES.USECASE,
      usecaseType: USECASE_TYPES.ACTOR,
      name: '管理员',
      x: 100,
      y: 400
    },
    {
      id: 'system1',
      type: ELEMENT_TYPES.USECASE,
      usecaseType: USECASE_TYPES.SYSTEM,
      name: '用户管理系统',
      x: 300,
      y: 300
    },
    {
      id: 'usecase1',
      type: ELEMENT_TYPES.USECASE,
      usecaseType: USECASE_TYPES.USECASE,
      name: '用户登录',
      x: 400,
      y: 200
    },
    {
      id: 'usecase2',
      type: ELEMENT_TYPES.USECASE,
      usecaseType: USECASE_TYPES.USECASE,
      name: '用户注册',
      x: 400,
      y: 250
    },
    {
      id: 'usecase3',
      type: ELEMENT_TYPES.USECASE,
      usecaseType: USECASE_TYPES.USECASE,
      name: '管理用户',
      x: 400,
      y: 350
    },
    {
      id: 'usecase4',
      type: ELEMENT_TYPES.USECASE,
      usecaseType: USECASE_TYPES.USECASE,
      name: '查看用户列表',
      x: 400,
      y: 400
    },
    {
      id: 'relationship1',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'actor1',
      targetId: 'usecase1',
      relationshipType: RELATIONSHIP_TYPES.ASSOCIATION
    },
    {
      id: 'relationship2',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'actor1',
      targetId: 'usecase2',
      relationshipType: RELATIONSHIP_TYPES.ASSOCIATION
    },
    {
      id: 'relationship3',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'actor2',
      targetId: 'usecase3',
      relationshipType: RELATIONSHIP_TYPES.ASSOCIATION
    },
    {
      id: 'relationship4',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'actor2',
      targetId: 'usecase4',
      relationshipType: RELATIONSHIP_TYPES.ASSOCIATION
    },
    {
      id: 'relationship5',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'usecase1',
      targetId: 'usecase2',
      relationshipType: RELATIONSHIP_TYPES.DEPENDENCY
    }
  ]
}

/**
 * 状态图模板
 */
export const STATE_DIAGRAM_TEMPLATE = {
  type: DIAGRAM_TYPES.STATE,
  elements: [
    {
      id: 'initial1',
      type: ELEMENT_TYPES.STATE,
      stateType: STATE_TYPES.INITIAL,
      name: '初始状态',
      x: 100,
      y: 200
    },
    {
      id: 'state1',
      type: ELEMENT_TYPES.STATE,
      stateType: STATE_TYPES.STATE,
      name: '空闲状态',
      x: 300,
      y: 200
    },
    {
      id: 'state2',
      type: ELEMENT_TYPES.STATE,
      stateType: STATE_TYPES.STATE,
      name: '处理中',
      x: 500,
      y: 200
    },
    {
      id: 'state3',
      type: ELEMENT_TYPES.STATE,
      stateType: STATE_TYPES.STATE,
      name: '完成状态',
      x: 700,
      y: 200
    },
    {
      id: 'final1',
      type: ELEMENT_TYPES.STATE,
      stateType: STATE_TYPES.FINAL,
      name: '结束状态',
      x: 900,
      y: 200
    },
    {
      id: 'relationship1',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'initial1',
      targetId: 'state1',
      relationshipType: RELATIONSHIP_TYPES.ASSOCIATION
    },
    {
      id: 'relationship2',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'state1',
      targetId: 'state2',
      relationshipType: RELATIONSHIP_TYPES.ASSOCIATION
    },
    {
      id: 'relationship3',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'state2',
      targetId: 'state3',
      relationshipType: RELATIONSHIP_TYPES.ASSOCIATION
    },
    {
      id: 'relationship4',
      type: ELEMENT_TYPES.RELATIONSHIP,
      sourceId: 'state3',
      targetId: 'final1',
      relationshipType: RELATIONSHIP_TYPES.ASSOCIATION
    }
  ]
}

/**
 * 获取模板数据
 * @param {string} diagramType - 图表类型
 * @returns {Object} 模板数据
 */
export function getTemplate(diagramType) {
  switch (diagramType) {
    case DIAGRAM_TYPES.CLASS:
      return CLASS_DIAGRAM_TEMPLATE
    case DIAGRAM_TYPES.ACTIVITY:
      return ACTIVITY_DIAGRAM_TEMPLATE
    case DIAGRAM_TYPES.SEQUENCE:
      return SEQUENCE_DIAGRAM_TEMPLATE
    case DIAGRAM_TYPES.USECASE:
      return USECASE_DIAGRAM_TEMPLATE
    case DIAGRAM_TYPES.STATE:
      return STATE_DIAGRAM_TEMPLATE
    default:
      return { type: diagramType, elements: [] }
  }
}



/**
 * 获取所有模板
 * @returns {Object} 所有模板数据
 */
export function getAllTemplates() {
  return {
    [DIAGRAM_TYPES.CLASS]: CLASS_DIAGRAM_TEMPLATE,
    [DIAGRAM_TYPES.ACTIVITY]: ACTIVITY_DIAGRAM_TEMPLATE,
    [DIAGRAM_TYPES.SEQUENCE]: SEQUENCE_DIAGRAM_TEMPLATE,
    [DIAGRAM_TYPES.USECASE]: USECASE_DIAGRAM_TEMPLATE,
    [DIAGRAM_TYPES.STATE]: STATE_DIAGRAM_TEMPLATE
  }
}

